//
//  ChooseResultCell.m
//  CarUser
//
//  Created by 王森林 on 2019/1/10.
//  Copyright © 2019年 王森林. All rights reserved.
//

#import "ChooseResultCell.h"
#import "ShopInfoOtherViewController.h"
#import "Car_CarDetailViewController.h"
@interface ChooseResultCell()
@property (nonatomic, strong) NSDictionary *resultDict;
@property  (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel  *typeLabel;
@property (nonatomic, strong) UILabel  *typeLabel1;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UIView *functionView;
@property (nonatomic, strong) UIButton *companyBtn;
@end

@implementation ChooseResultCell

- (void)setData:(id)data
{
    
    self.resultDict = [NSDictionary dictionaryWithDictionary:data];
    [self createUIWithData];
    
}

- (void)handleTapView:(UIGestureRecognizer *)gesture
{
//    HomeCarDetailViewController *push = [[HomeCarDetailViewController alloc]init];
//    push.SALECAR_ID = [self.resultDict objectForKey:@"SALECAR_ID"];
//    [[self viewController].navigationController pushViewController:push animated:YES];
    
    Car_CarDetailViewController *push = [[Car_CarDetailViewController alloc]init];
//    NSDictionary  *dict = self.carHotAry[indexPath.row - 5];
    push.SERIES_ID = [self.resultDict objectForKey:@"SALECAR_ID"];
    [[self  viewController].navigationController pushViewController:push animated:YES];
    
}

- (void)handleBtnAction:(id)sender
{
    ShopInfoOtherViewController *controller = [[ShopInfoOtherViewController alloc] init];
  
    controller.TRADERS_ID = [self.resultDict objectForKey:@"TRADERS_ID"];
   
    [[self viewController].navigationController pushViewController:controller animated:YES];
    
}


- (void)createUIWithData
{
    [self.contentView removeAllSubviews];
    self.bgView = nil;
    self.functionView = nil;
    self.companyBtn = nil;
    self.contentView.backgroundColor = RGB(234, 234, 234);
    [self.contentView addSubview:self.bgView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapView:)];
    [self.bgView addGestureRecognizer:tap];
    [self.bgView addSubview:self.iconImageView];
    [self.bgView addSubview:self.titleLabel];
    [self.bgView addSubview:self.typeLabel];
    [self.bgView addSubview:self.typeLabel1];
    [self.bgView addSubview:self.priceLabel];
    [self.bgView addSubview:self.dateLabel];
    [self.bgView addSubview:self.functionView];
    [self.bgView addSubview:self.companyBtn];
    
    //    [self.iconImageView setImage:IMG(@"car_test1")];
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:APPEND_URL(BASE_URL, [self.resultDict objectForKey:@"ZST000"])]];
    self.titleLabel.text = [self.resultDict objectForKey:@"CXMC00"];
    self.typeLabel.text = [self.resultDict objectForKey:@"GGBBMC"];
    self.typeLabel1.text = [self.resultDict objectForKey:@"GHFSMC"];
    //    self.priceLabel.text = @"¥28.18万";
    self.priceLabel.text = [NSString stringWithFormat:@"¥%@万",[self.resultDict objectForKey:@"ZDJG00"]];
     self.dateLabel.text = [NSString stringWithFormat:@"%@",[self.resultDict objectForKey:@"SJRQ00"]];
    
    [self.functionView removeAllSubviews];
    self.functionView.backgroundColor = Common_BackgroundGrayColor;
    NSMutableArray *functionAry = [NSMutableArray new];
    [functionAry addObjectsFromArray:[self.resultDict objectForKey:@"CKPZ00"]];
    [functionAry addObjectsFromArray:[self.resultDict objectForKey:@"SSPZ00"]];
    [functionAry addObjectsFromArray:[self.resultDict objectForKey:@"AQPZ00"]];
//    float count = functionAry.count;
    if (functionAry.count % 2 == 0) {
        self.functionView.frame = CGRectMake(15, 110, BOUNDWIDTH-30, 20+20*(functionAry.count/2));
    } else {
        self.functionView.frame = CGRectMake(15, 110, BOUNDWIDTH-30, 20+20*(functionAry.count/2+1));
    }
   
    [functionAry enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *dict = functionAry[idx];
        
        UIView *functionBGView = [[UIView alloc] initWithFrame:CGRectMake(0+(BOUNDWIDTH-30)/2*(idx%2), 10+20*(idx/2), (BOUNDWIDTH-30)/2, 15)];
        [self.functionView addSubview:functionBGView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,(15-4)/2 , 4, 4)];
        label.backgroundColor = [UIColor blackColor];
        label.layer.cornerRadius = 4/2;
        label.layer.masksToBounds = YES;
        [functionBGView addSubview:label];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, (BOUNDWIDTH-30)/2-20, 15)];
        titleLabel.textColor = RGB(102, 102, 102);
        titleLabel.font = FONT_SIZE(14);
        titleLabel.text = [dict objectForKey:@"NAME"];
        [functionBGView addSubview:titleLabel];
        
    }];
    
     [self createTopView];
    
//    [self.companyBtn setTitle:[self.resultDict objectForKey:@"JXSMC0"] forState:UIControlStateNormal];
//
//    UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake(BOUNDWIDTH-40, 0, 7, 14)];
//    arrowImageView.image = [UIImage imageNamed:@"home_arrow"];
//    [self.companyBtn addSubview:arrowImageView];
    
    [self createConstraint];
    
}

- (void)createTopView
{
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
    imageView1.image = IMG(@"cardetail_1");
    [self.companyBtn addSubview:imageView1];
    
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(25, 0, 15, 15)];
    imageView2.image = IMG(@"cardetail_2");
    [self.companyBtn  addSubview:imageView2];
    
    
    
    UILabel  *companyLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, 0, BOUNDWIDTH - 80, 15)];
    companyLabel.textColor = RGB(68, 68, 68);
    companyLabel.font = B_FONT_SIZE(12);
    companyLabel.text = [self.resultDict objectForKey:@"JXSMC0"];
    //    companyLabel.text = [infoDict objectForKey:@"JXSMC0"];
    //    companyLabel.text = [self.orderDict objectForKey:@"JXSMC1"];
    
    //    if (self.type.intValue == 1) {
    //        companyLabel.text = [self.orderDict objectForKey:@"JXSMC0"];
    //    }
    //    if (self.type.intValue == 2) {
    //        companyLabel.text = [self.orderDict objectForKey:@"JXSMC1"];
    //    }
    //
    //    if (self.type.intValue == 3) {
    //        companyLabel.text = [self.orderDict objectForKey:@"JXSMC1"];
    //    }
    [self.companyBtn  addSubview:companyLabel];
    //
}


-(void)createConstraint
{
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(0);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-5);
        (void)make.left.and.right;
        
    }];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).offset(10);
        make.left.equalTo(self.contentView.mas_left).offset(15);
        make.width.mas_equalTo(140);
        make.height.mas_equalTo(90);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(15);
        make.left.equalTo(self.iconImageView.mas_right).offset(8);
        make.right.equalTo(self.contentView.mas_right).offset(-10);
    }];
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        make.left.equalTo(self.iconImageView.mas_right).offset(8);
        make.width.mas_equalTo(38);
        make.height.mas_equalTo(18);
    }];
    
    [self.typeLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        make.left.equalTo(self.typeLabel.mas_right).offset(10);
        make.width.mas_equalTo(38);
        make.height.mas_equalTo(18);
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.iconImageView.mas_bottom).offset(0);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(15);
        make.left.equalTo(self.iconImageView.mas_right).offset(8);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.iconImageView.mas_bottom).offset(0);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(17);
        make.right.equalTo(self.bgView.mas_right).offset(-16);
    }];
    
    [self.companyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.functionView.mas_bottom).offset(10);
        make.left.mas_equalTo(self.bgView.mas_left).offset(15);
        make.right.mas_equalTo(self.bgView.mas_right).offset(-15);
        make.height.mas_equalTo(15);
    }];
    
    
    [self.companyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bgView).offset(-10);
    }];

}



- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
      
        
    }
    return _bgView;
}

- (UIImageView *)iconImageView
{
    if (!_iconImageView) {
        _iconImageView = [UIImageView new];
        
    }
    return _iconImageView;
}


- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = Common_TextColor;
        _titleLabel.font = FONT_SIZE(14);
        _titleLabel.numberOfLines = 0;
    }
    return _titleLabel;
    
}

- (UILabel *)typeLabel
{
    if (!_typeLabel) {
        _typeLabel = [UILabel new];
        _typeLabel.textColor = Common_Text_TextfieldColor;
        _typeLabel.textAlignment = NSTextAlignmentCenter;
        _typeLabel.font = FONT_SIZE(12);
        _typeLabel.layer.borderWidth = 1;
        _typeLabel.layer.cornerRadius = 6;
        _typeLabel.layer.masksToBounds = YES;
        _typeLabel.layer.borderColor = Common_Head_Color.CGColor;
    }
    return _typeLabel;
    
}

- (UILabel *)typeLabel1
{
    if (!_typeLabel1) {
        _typeLabel1 = [UILabel new];
        _typeLabel1.textColor = Common_Text_TextfieldColor;
        _typeLabel1.textAlignment = NSTextAlignmentCenter;
        _typeLabel1.font = FONT_SIZE(12);
        _typeLabel1.layer.borderWidth = 1;
        _typeLabel1.layer.cornerRadius = 6;
        _typeLabel1.layer.masksToBounds = YES;
        _typeLabel1.layer.borderColor = Common_Head_Color.CGColor;
    }
    return _typeLabel1;
    
}
- (UILabel *)priceLabel
{
    if (!_priceLabel) {
        _priceLabel = [UILabel new];
        _priceLabel.textColor = Common_TextColor;
        _priceLabel.textAlignment = NSTextAlignmentRight;
        _priceLabel.font = B_FONT_SIZE(18);
        
    }
    return _priceLabel;
    
}


- (UILabel *)dateLabel
{
    if (!_dateLabel) {
        _dateLabel = [UILabel new];
        _dateLabel.textColor = Common_TextLowColor;
        _dateLabel.textAlignment = NSTextAlignmentLeft;
        _dateLabel.font = FONT_SIZE(12);
    }
    return _dateLabel;
    
}

- (UIView *)functionView
{
    if (!_functionView) {
        _functionView = [UIView new];
        _functionView.layer.cornerRadius = 5;
        _functionView.layer.masksToBounds = YES;
    }
    return _functionView;
    
}

- (UIButton *)companyBtn
{
    if (!_companyBtn) {
        _companyBtn = [UIButton new];
        _companyBtn.titleLabel.font = FONT_SIZE(12);
        [_companyBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
//        _companyBtn.titleLabel.textAlignment= NSTextAlignmentLeft;
        _companyBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
         [_companyBtn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _companyBtn;
    
}


@end
